package com.hejia.alauda.service;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.BankCard;
import com.hejia.alauda.entity.Order;
import com.hejia.alauda.entity.PayGate;
import com.hejia.alauda.entity.PayOrder;
import com.hejia.alauda.entity.Product;
import com.hejia.alauda.entity.User;
import com.hejia.alauda.entity.example.BonusRecordExample;
import com.hejia.alauda.entity.example.OrderExample;
import com.hejia.alauda.entity.example.PayOrderExample;
import com.hejia.alauda.persistence.mybatis.BonusRecordMapper;
import com.hejia.alauda.persistence.mybatis.OrderMapper;
import com.hejia.alauda.persistence.mybatis.PayOrderMapper;

@Service
public class RookieServiceImpl implements RookieService {

	@Resource
	private PayOrderMapper payOrderMapper;
	
	@Resource
	private PaymentService paymentService;
	
	@Resource
	private CommonService commonService;
	
	@Resource
	private BonusRecordMapper bonusRecordMapper;
	
	@Resource
	private RedEnvelopeService redEnvelopeService;
	
	@Resource
	private BonusService bonusService;
	
	@Resource
	private OrderMapper orderMapper;
	
	@Override
	public Product createRookieProduct() {
		
		DateTime now = new DateTime();
		Product product = new Product();
		product.setName("理财高手 速成1号");
		product.setTag(2);
		product.setStatus(Product.Status.PUBLISHED);
		product.setSerial(RookieService.ROOKIE_PRODUCT_SERIAL);
		product.setTerm(7);
		product.setInterestRate(BigDecimal.valueOf(0.08));
		product.setBonusRate(BigDecimal.ZERO);
		product.setMinAmount(BigDecimal.valueOf(1));
		product.setMaxAmount(BigDecimal.valueOf(9999));
		product.setTotalAmount(BigDecimal.valueOf(1000000));
		product.setSoldAmount(BigDecimal.ZERO);
		product.setUnpayAmount(BigDecimal.ZERO);
		product.setRookie(false);
		product.setStartTime(now.toDate());
		product.setEndTime(now.plusDays(1).toDate());
		
		return product;
	}

	@Override
	public Order createRookieOrder(User user, Product product, String source, PayGate paygate, BankCard bankCard) {
		
		if (this.isExperienced(user.getId())) {
			throw new RuntimeException("您已经参与过新手体验");
		}
		
		// 创建支付订单
		String payNumber = commonService.getOrderSerial().replace("DD", PayOrder.PAY_NUMBER_PREFIX);
		PayOrder payOrder = new PayOrder();
		payOrder.setCreateDate(new Date());
		payOrder.setType(PayOrder.Type.ROOKIE_EXPERIENCE.toString());
		payOrder.setUser(user);
		payOrder.setBankCard(bankCard);
		payOrder.setSource(source);
		payOrder.setPayGateCode(paygate.getGateCode());
		payOrder.setDescription(product.getName().trim());
		payOrder.setAmount(BigDecimal.valueOf(0.01));
		payOrder.setPayNumber(payNumber);
		payOrder.setStatus(PayOrder.Status.STATUS_UNPAY.toString());
		paymentService.addPayOrder(payOrder);
		
		// 创建模拟订单
		Order order = new Order();
		order.setCreateDate(payOrder.getCreateDate());
		order.setModifyDate(payOrder.getModifyDate());
		order.setUser(payOrder.getUser());
		order.setProduct(product);
		order.setSource(source);
		order.setPayGateCode(payOrder.getPayGateCode());
		order.setAmount(payOrder.getAmount());
		order.setOrderNumber(payOrder.getPayNumber());
		order.setPayNumber(payOrder.getPayNumber());
		
		return order;
	}

	@Override
	public boolean isExperienced(Long userId) {
		
		// 是否购买过
		OrderExample orderExample = new OrderExample();
		orderExample.appendCriterion("user_id =", userId);
		orderExample.appendCriterion("status = 'STATUS_PAID'");
		if (this.orderMapper.countByExample(orderExample) > 0) {
			return true;
		}
		
		// 是否已参加新手体验
		PayOrderExample example = new PayOrderExample();
		example.appendCriterion("user_id =", userId);
		example.appendCriterion("type =", PayOrder.Type.ROOKIE_EXPERIENCE.toString());
		example.appendCriterion("status =", PayOrder.Status.STATUS_PAID.toString());
		return this.payOrderMapper.countByExample(example) > 0;
	}

	@Override
	public void addRookieExperienceBonus(PayOrder payOrder) {
		if (!payOrder.getStatus().equals(PayOrder.Status.STATUS_PAID.toString())) {
			return;
		}
		
		String code = "ROOKIE_EXPERIENCE";
		// 是否已发放
		BonusRecordExample example = new BonusRecordExample();
		example.appendCriterion("code =", code);
		example.appendCriterion("user_id =", payOrder.getUser().getId());
		if (bonusRecordMapper.countByExample(example) > 0){
			return;
		}
		
		// 红包金额=收益+1分支付
		Product product = this.createRookieProduct();
		BigDecimal amount = product.getInvestmentIncome(product.getMaxAmount()).add(BigDecimal.valueOf(0.01));
		this.bonusService.addBonusRecordByRedpacket(payOrder.getUser(), code, amount);
	}

}
